package org.bonorsoft.modules.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.bonorsoft.common.api.vo.Result;
import org.bonorsoft.common.aspect.annotation.AutoLog;
import org.bonorsoft.common.system.base.controller.BonorController;
import org.bonorsoft.common.system.query.QueryGenerator;
import org.bonorsoft.modules.user.entity.MobileUserDepart;
import org.bonorsoft.modules.user.service.IMobileUserDepartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;

@Slf4j
@RestController
@RequestMapping("/mobile/userDepart")
public class MobileUserDepartController extends BonorController<MobileUserDepart, IMobileUserDepartService> {
    @Autowired
    private IMobileUserDepartService mobileUserDepartService;

    /**
     * 分页列表查询
     *
     * @param mobileUserDepart
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "用户与部门关联-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(MobileUserDepart mobileUserDepart,
                                   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<MobileUserDepart> queryWrapper = QueryGenerator.initQueryWrapper(mobileUserDepart, req.getParameterMap());
        Page<MobileUserDepart> page = new Page<MobileUserDepart>(pageNo, pageSize);
        IPage<MobileUserDepart> pageList = mobileUserDepartService.page(page, queryWrapper);
        return Result.ok(pageList);
    }

    /**
     * 添加
     *
     * @param mobileUserDepart
     * @return
     */
    @AutoLog(value = "用户与部门关联-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody MobileUserDepart mobileUserDepart) {
        mobileUserDepartService.save(mobileUserDepart);
        return Result.ok("添加成功！");
    }

    /**
     * 编辑
     *
     * @param mobileUserDepart
     * @return
     */
    @AutoLog(value = "用户与部门关联-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody MobileUserDepart mobileUserDepart) {
        mobileUserDepartService.updateById(mobileUserDepart);
        return Result.ok("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "用户与部门关联-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name="id",required=true) String id) {
        mobileUserDepartService.removeById(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "用户与部门关联-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
        this.mobileUserDepartService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功！");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "用户与部门关联-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
        MobileUserDepart mobileUserDepart = mobileUserDepartService.getById(id);
        return Result.ok(mobileUserDepart);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param mobileUserDepart
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, MobileUserDepart mobileUserDepart) {
        return super.exportXls(request, mobileUserDepart, MobileUserDepart.class, "用户与部门关联");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, MobileUserDepart.class);
    }
}
